<template>
  <div>
    <el-form ref="editPram" :model="editPram" label-width="130px">
      <div v-if="this.biztype.value === 3">
        <el-form-item
          label="单位名称"
          prop="name"
          :rules="[{ required:true,message:'请输入单位名称',trigger:['blur','change'] }]"
        >
          <el-input v-model="editPram.name" maxlength="20" placeholder="单位名称" />
        </el-form-item>
        <el-form-item
          label="单位简称"
          prop="shortName"
          :rules="[{ required:true,message:'请输入单位简称',trigger:['blur','change'] }]"
        >
          <el-input v-model="editPram.shortName" maxlength="20" placeholder="单位简称" />
        </el-form-item>
<!--        <el-form-item label="上级单位" prop="pid">-->
<!--          <el-cascader v-model="editPram.pid" :disabled="isCreate ===1 && editPram.pid ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />-->
          <!-- allTreeList or parentOptions-->
<!--        </el-form-item>-->
<!--        <el-form-item label="单位级别" prop="level" :rules="[{ required:true,message:'请选择单位级别',trigger:['blur','change'] }]">-->
<!--          <el-select v-model="editPram.level" placeholder="单位级别" clearable style="width: 100%">-->
<!--            <el-option-->
<!--              v-for="(item,index) in levelList"-->
<!--              :key="index"-->
<!--              :label="item.levelName"-->
<!--              :value="item.id"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="是否存在干部">
          <el-radio-group v-model="editPram.isCards">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="editPram.sort" :min="0"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="editPram.status"  active-text="显示"
                     inactive-text="隐藏" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loadingBtn" @click="handlerSubmit('editPram')" v-hasPermi="['admin:department:save']">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </div>
      <div v-if="this.biztype.value !== 3">
        <el-form-item
          label="配置名称"
          prop="name"
          :rules="[{ required:true,message:'请输入配置名称',trigger:['blur','change'] }]"
        >
          <el-input v-model="editPram.name" maxlength="20" placeholder="配置名称" />
        </el-form-item>
        <el-form-item label="父级">
          <el-cascader v-model="editPram.pid" :disabled="isCreate ===1 && editPram.pid ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
          <!-- allTreeList or parentOptions-->
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="editPram.sort" :min="0"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="editPram.status"  active-text="显示"
                     inactive-text="隐藏" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loadingBtn" @click="handlerSubmit('editPram')" v-hasPermi="['admin:system:category:save']">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<!--创建和编辑公用一个组件-->
<script>
import * as categoryApi from '@/api/categoryApi.js'
import * as roleApi from '@/api/role.js'
import * as selfUtil from '@/utils/ZBKJIutil.js'
import {addCategroyForDepartment} from "@/api/categoryApi.js";
export default {
  // name: "edit"
  props: {
    prent: {
      type: Object,
      required: true
    },
    isCreate: {
      type: Number,
      default: 0
    },
    biztype: {
      type: Object,
      required: true
    },
    editData: {
      type: Object
    },
    allTreeList: {
      type: Array
    }
  },
  data() {
    return {
      loadingBtn: false,
      constants: this.$constants,
      editPram: {
        name: null,
        shortName: null,
        pid: null,
        sort: 0,
        status: 1,
        isCards: 0,
        //level: null,
        id: 0
      },
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false
      },
      parentOptions: [],
      //levelList: [],
    }
  },
  mounted() {
    this.initEditData()
    //this.handleGetLevelList()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    /*handleGetLevelList() {
      //const _pram = {
        //page: 1,
        //limit: this.constants.page.limit[4],
      //}
      roleApi.getCommonLevel().then(data => {
        this.levelList = data;
        let arr = [];
        data.forEach(item=>{
          arr.push(item.id);
        })

        if(!arr.includes(Number.parseInt(this.editPram.level))){
          this.$set(this.editPram,'level',null);
        }
      })
    },*/
    initEditData() {
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')
      const { name, shortName, pid, sort, status, id, isCards } = this.editData
      if(this.isCreate === 1){
        this.editPram.name = name
        this.editPram.shortName = shortName
        this.editPram.pid = pid
        this.editPram.sort = sort
        this.editPram.status = status
        this.editPram.isCards = isCards
        //this.editPram.level = level
        this.editPram.id = id
      }else{
        this.editPram.pid = this.prent.id
      }
    },
    //处理哪一级别禁用
    addTreeListLabelForCasCard(arr, child) {
      if(this.biztype.value === 3){
        arr.forEach((o,i) => {
          if (o.child && o.child.length) {
            // o.disabled = true
            o.child.forEach((j) => {
              j.disabled = true
              if (j.child && j.child.length) {
                j.child.forEach((k) => {
                  //k.disabled = true
                })
              }
            })
          }
        })
      }else{
        arr.forEach((o,i) => {
          if (o.child && o.child.length) {
            // o.disabled = true
            o.child.forEach((j) => {
              j.disabled = true
            })
          }
        })
      }

    },
    handlerSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return
        this.handlerSaveOrUpdate(this.isCreate === 0)
      })
    },
    handlerSaveOrUpdate(isSave) {
      if (isSave) {
        //this.editPram.pid = this.prent.id
        this.loadingBtn = true
        categoryApi.addCategroy(this.editPram,this.biztype.value).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('创建成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      } else {
        this.editPram.pid = Array.isArray(this.editPram.pid) ? this.editPram.pid[0] : this.editPram.pid
        this.loadingBtn = true
        categoryApi.addCategroy(this.editPram,this.biztype.value).then(data => {
          this.$emit('hideEditDialog')
          this.$message.success('更新成功')
          this.loadingBtn = false
        }).catch(() => {
          this.loadingBtn = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
